package com.rayc.mall.common.context;

/**
 * @Author: Ray-C
 * @CreateTime: 2025-03-23
 * @Description: 用户信息上下文
 * @Version: 1.0
 */
public class UserContext {
    private static final ThreadLocal<Long> THREAD_LOCAL = new ThreadLocal<>();

    /**
     * 保存当前登录用户信息到ThreadLocal
     *
     * @param userId 用户id
     */
    public static void setUser(Long userId) {
        THREAD_LOCAL.set(userId);
    }

    /**
     * 获取当前登录用户信息
     *
     * @return 用户id
     */
    public static Long getUser() {
        return THREAD_LOCAL.get();
    }

    /**
     * 移除当前登录用户信息
     */
    public static void removeUser() {
        THREAD_LOCAL.remove();
    }
}
